<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Column;

class ColumnController extends Controller
{
    /**
     * 栏目列表显示
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $columns = Column::get();
        return view('admin.column.index',['title'=>'栏目列表', 'columns'=>$columns]);
    }

    /**
     * 添加栏目页面显示
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.column.create',['title'=>'添加栏目']);
    }

    /**
     * 执行添加栏目操作
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'name' => 'required|regex:/^\S{6,18}$/',
            'logo' => 'required',
        ], [
            'name.regex' => '栏目名不符合规则',
            'name.required' => '栏目名不能为空',
            'logo.required' => '栏目logo不能为空',
        ]);
        $column = new Column;
        $column->title = $request->name;
        if ($request->hasFile('logo')) {
            //上传文件夹的路径
            $dirname = './uploads/logo/' . date('Ymd') . '/';
            //获取文件的后缀
            $suffix = $request->file('logo')->getClientOriginalExtension();
            //文件名
            $name = time() . rand(100000, 999999) . '.' . $suffix;
            $request->file('logo')->move($dirname, $name);
            //存入对象中(数据表中)
            $column->logo = trim($dirname, '.') . $name;
        }
        $res = $column->save() ? 'success' : 'error';
        return back()->with($res,1);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $column = Column::find($id);
        return view('admin.column.edit',['title'=>'编辑栏目','column'=>$column]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'name' => 'required|regex:/^\S{6,18}$/',
        ], [
            'name.regex' => '栏目名不符合规则',
            'name.required' => '栏目名不能为空',
        ]);
        $column = Column::find($id);
        $column->title = $request->name;
        if ($request->hasFile('logo')) {
            //上传文件夹的路径
            $dirname = './uploads/logo/' . date('Ymd') . '/';
            //获取文件的后缀
            $suffix = $request->file('logo')->getClientOriginalExtension();
            //文件名
            $name = time() . rand(100000, 999999) . '.' . $suffix;
            $request->file('logo')->move($dirname, $name);
            //存入对象中(数据表中)
            $column->logo = trim($dirname, '.') . $name;
        }
        $res = $column->save() ? 'success' : 'error';
        return back()->with($res,1);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $column = Column::find($id);
        $res = $column->delete() ? 'success' : 'error';
        return back()->with($res,1);
    }
}
